package ReadFileCustomer;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
/**
You have a file (ex: transactions.txt) with the following content:

C1,P1,100
C1,P2,50
C1,P3,70
C1,P4,10
C3,P4,10
C2,P1,100
C2,P5,35

Where Cn - is customer or account, Pn - product type, Amount.

write a program which will parse this file and print the following information:

cmd> java TransactionParser transactions.txt

Account Total
C1      230
C2      135
C3      10

Product Quantity
P1      2x100
P2      1x50
P3      1x70
P4      2x10
P5      1x35

Most expensive Product: P1

Total: 375

Note: these should be ordered descending.

*/
public class TestFileReader {

	public static void main(String[] args) throws FileNotFoundException {
		File f = new File("lista.txt");
		ArrayList<String> lista = new ArrayList<String>();

		Scanner scaner = new Scanner(f);

		// incarcam lista rind cu rind
		while (scaner.hasNext()) {
			lista.add(scaner.nextLine());

		}
		scaner.close();

		ArrayList<Customer> listaCustomeri = new ArrayList<Customer>();

		for (String el : lista) {
			Customer customer = new Customer();
			Produse produse = new Produse();
			String[] elRind = el.split(",");

			for (int i = 0; i < elRind.length; i++) {

				String s = elRind[i];

				switch (i) {

				case 0:
					customer.setNumeCustomer(s);
					break;
				case 1:
					produse.setNumeProdus(s);
					break;
				case 2:
					produse.setPret(Integer.parseInt(s));
					break;

				}

			}
			customer.setProdus(produse);
			listaCustomeri.add(customer);

		}

		Set<String> cumparatoriUnici = new TreeSet<String>();

		for (Customer c : listaCustomeri) {

			String nume = c.getNumeCustomer();
			cumparatoriUnici.add(nume);

		}

		Set<String> produseUnice = new TreeSet<String>();

		for (Customer c : listaCustomeri) {

			String nume = c.getProdus().getNumeProdus();
			produseUnice.add(nume);

		}

		Map<String, Integer> customerPret = new TreeMap<String, Integer>();

		Integer achitare = 0;
		for (String el : cumparatoriUnici) {
			Integer total = 0;

			for (Customer c : listaCustomeri) {
				if (el.equals(c.getNumeCustomer())) {

					total += c.getProdus().getPret();

				}

			}
			achitare += total;
			customerPret.put(el, total);

		}

		Map<String, String> produsCantit = new TreeMap<String, String>();
		ArrayList<Integer> pretulProduselor = new ArrayList<Integer>();

		for (String el : produseUnice) {

			Integer cant = 0;
			Integer pret = 0;

			for (Customer c : listaCustomeri) {
				if (el.equals(c.getProdus().getNumeProdus())) {
					pretulProduselor.add(c.getProdus().getPret());
					cant += 1;
					pret = c.getProdus().getPret();

				}

			}
			produsCantit.put(el, cant + "x" + pret);

		}
		Integer pretScump = Collections.max(pretulProduselor);
		String produsulCelMaiScump = "";
		for (Customer c : listaCustomeri) {
			Integer test = c.getProdus().getPret();

			if (pretScump.equals(test)) {

				produsulCelMaiScump = c.getProdus().getNumeProdus();

			}
		}

		displayCustomer("Customeri     Pret \n", customerPret);
		displayProdus("Produs     Cant x Pret \n", produsCantit);


		System.out.println("\n Pretul totalul este: " + achitare + "\n");

		System.out.println("\n Produsul cel mai scump este: "
				+ produsulCelMaiScump + "\n");

	}

	public static void displayCustomer(String s, Map<String, Integer> map)

	{
		System.out.println();
		System.out.print(s);
		System.out.println();
		for (String lel : map.keySet()) {

			System.out.print(lel + "           ");
			System.out.print(map.get(lel));

			System.out.println();

		}

	}

	public static void displayProdus(String s, Map<String, String> map)

	{
		System.out.println();
		System.out.print(s);
		System.out.println();
		for (String lel : map.keySet()) {

			System.out.print(lel + "           ");
			System.out.print(map.get(lel));

			System.out.println();

		}

	}

}
